using System.Diagnostics.Contracts;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame1
{
	public struct MyOwnVertexFormat
	{
		public Vector3 position { get; set; }
		public Color color { get; set; }
		//public Vector3 normal { get; set; }

		public static VertexDeclaration VertexDeclaration = new VertexDeclaration
			(
			new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
			new VertexElement(1 * sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0)
			//new VertexElement(2 * sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
			);
	}

	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public partial class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		GraphicsDevice device { get { return graphics.GraphicsDevice; } }
		SpriteBatch spriteBatch;

		GameObject ground;
		Camera gameCamera;

		KeyboardState lastKeyboardState = new KeyboardState();
		KeyboardState currentKeyboardState = new KeyboardState();

		MouseState originalMouseState;

		private VertexBuffer groundGrid;
		private BasicEffect basicEffect;
		private VertexBuffer vertexBuffer;

		const float color_hi = 1.0f;
		const float color_low = 0.5f;
		Color color_white = new Color(color_hi, color_hi, color_hi);
		Color color_red = new Color(color_hi, color_low, color_low);
		Color color_green = new Color(color_low, color_hi, color_low);
		Color color_blue = new Color(color_low, color_low, color_hi);


		public Game1()
		{
			graphics = new GraphicsDeviceManager(this);
			Content.RootDirectory = "Content";
		}

		/// <summary>
		/// Allows the game to perform any initialization it needs to before starting to run.
		/// This is where it can query for any required services and load any non-graphic
		/// related content.  Calling base.Initialize will enumerate through any components
		/// and initialize them as well.
		/// </summary>
		protected override void Initialize()
		{
			// TODO: Add your initialization logic here

			Contract.Assert(true);

			Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2);
			originalMouseState = Mouse.GetState();

			ground = new GameObject();
			gameCamera = new Camera
							{
								AspectRatio = GraphicsDevice.Viewport.AspectRatio,
							};
			gameCamera.Update();

			ground.Model = Content.Load<Model>("Models/ground");

			//float rotation = 0.0f;
			//Vector3 position = new Vector3 { X = 10, Y = 10, Z = 0 };
			//gameCamera.TargetOffset = new Vector3 { X = 0, Y = 0, Z = 0 };
			//gameCamera.Update(rotation, position, GraphicsDevice.Viewport.AspectRatio);

			groundGrid = CreateGroundGrid();

			basicEffect = new BasicEffect(device)
							{
								VertexColorEnabled = true,
								PreferPerPixelLighting = true,
							};
			//basicEffect.AmbientLightColor = Vector3.One;
			//basicEffect.LightingEnabled = true;

			var vertices = new[]
			               	{
			               		new MyOwnVertexFormat {position = new Vector3(0, 0, 0), color = color_white},//, normal = Vector3.UnitY},
			               		new MyOwnVertexFormat {position = new Vector3(1, 0, 1), color = color_green},//, normal = Vector3.UnitY},
			               		new MyOwnVertexFormat {position = new Vector3(1, 0, 0), color = color_green},//, normal = Vector3.UnitY},
			               	};


			vertexBuffer = new VertexBuffer(device, MyOwnVertexFormat.VertexDeclaration, vertices.Length,
											BufferUsage.WriteOnly);

			vertexBuffer.SetData(vertices);

			base.Initialize();
		}

		private VertexBuffer CreateGroundGrid()
		{
			var vertices = new[]
			               	{
			               		new MyOwnVertexFormat {position = new Vector3(0, 0, 0), color = color_green},
			               		new MyOwnVertexFormat {position = new Vector3(1, 0, 0), color = color_green},
			               		new MyOwnVertexFormat {position = new Vector3(0, 0, 0), color = color_blue},
			               		new MyOwnVertexFormat {position = new Vector3(0, 1, 0), color = color_blue},
			               		new MyOwnVertexFormat {position = new Vector3(0, 0, 0), color = color_red},
			               		new MyOwnVertexFormat {position = new Vector3(0, 0, 1), color = color_red},
			               	};


			var vertexBuffer = new VertexBuffer(device, MyOwnVertexFormat.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);

			vertexBuffer.SetData(vertices);

			/*
			
			var vertexes2 = new VertexPositionColor[]
			               	{
								new VertexPositionColor{Position = new Vector3(0, 0, 0), Color = color_white}, 
								new VertexPositionColor{Position = new Vector3(1, 0, 0), Color = color_green}, 
								new VertexPositionColor{Position = new Vector3(1, 0, 1), Color = color_green}, 
			               	};
			var vertexes = new[]
			               	{
								new VertexPositionNormalTexture{Position = new Vector3(0, 0, 0), Normal = Vector3.UnitY}, 
								new VertexPositionNormalTexture{Position = new Vector3(1, 0, 0), Normal = Vector3.UnitY}, 
								new VertexPositionNormalTexture{Position = new Vector3(1, 0, 1), Normal = Vector3.UnitY}, 
			               	};

			*/
			return vertexBuffer;
		}

		/// <summary>
		/// LoadContent will be called once per game and is the place to load
		/// all of your content.
		/// </summary>
		protected override void LoadContent()
		{
			// Create a new SpriteBatch, which can be used to draw textures.
			spriteBatch = new SpriteBatch(GraphicsDevice);

			// TODO: use this.Content to load your game content here
		}

		/// <summary>
		/// UnloadContent will be called once per game and is the place to unload
		/// all content.
		/// </summary>
		protected override void UnloadContent()
		{
			// TODO: Unload any non ContentManager content here
		}



	}
}
